import pandas as pd
class signal_merge:
    def __init__(self,timeperiod=1,merge="and"):
        self.timeperiod=timeperiod
        if merge=="and":
            self.oncalc=self.signal_and
        else:
            self.oncalc = self.signal_or
        self.his=[]
    def signal_and(self,*s,timekey=None):
        rst = 0
        self.his.append(s)
        if len(self.his)>self.timeperiod:
            self.his.pop(0)
        if len(self.his)==self.timeperiod:
            ss=pd.DataFrame(self.his)
            is1 = []
            is2 = []
            for i in range(len(s)):
                v=ss[i].to_list()
                i20 = -2 in v
                i21 = 2 in v
                is1.append(i20)
                is2.append(i21)
            if False not in is1:
                return -2
            elif False not in is2:
                return 2
        return rst
    def signal_or(self,*s,timekey=None):
        rst=0
        a1=s.count(2)
        a2=s.count(-2)
        if a1>0 and a2>0:
            if a1>a2:
                return 2
            if a2>a1:
                return -2
        elif a1>0 and a2==0:
            return 2
        elif a1==0 and a2>0:
            return -2
        return rst

if __name__ == '__main__':
    signal_merge0 = signal_merge(timeperiod=2,merge="and")
    signal_merge1 = signal_merge(timeperiod=2, merge="or")
    for i in [[0,2],[2,0],[0,0]]:
        print("signal_merge0",signal_merge0.signal_and(i[0],i[1]))
    for i in [[0,2],[2,0],[0,0]]:
        print("signal_merge1",signal_merge1.signal_or(i[0],i[1]))
